package com.cloudptt.api.product.operator;

import java.io.File;
import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.pjsip.api.MessageInfo;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DBAdapter;
import com.mcptt.db.loader.msg.SessionFuncListLoader;
import com.mcptt.db.loader.msg.SessionListLoader;
import com.mcptt.db.loader.msg.SessionListLoaderCb;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;
import com.mcptt.db.model.HistoryInfo;
import com.mcptt.db.model.MemberInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.model.SessionReadStatus;
import com.mcptt.http.operator.IHttpOpCb;
import com.mcptt.http.operator.file.DownloadFileLoader;
import com.mcptt.http.operator.file.OnDownloadListener;
import com.mcptt.http.operator.file.OnUploadListener;
import com.mcptt.http.operator.file.UploadFileLoader;

import android.content.Context;

public class MsgOperator extends SipOperator
{
	private final static String THIS_FILE = "MsgOperator";
//	private boolean isAuthorize_Data = false;

	private DBAdapter dbOperater = null;
	public MsgOperator(Context ctx, DBAdapter dbOperater)
	{
		super(ctx);
		this.dbOperater = dbOperater;
	}

//	public void onPublish(int result)
//	{
//		Log.d(THIS_FILE,  "onPublish result = " + result);
//		if((result & UserInfo.McType_Data)  == UserInfo.McType_Data)
//		{
//			isAuthorize_Data = true;
//		}
//		else
//		{
//			isAuthorize_Data = false;
//		}
//	}

	public int checkPermission()
	{
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		boolean hasMcData = checkPublishResult(CallAttr.McType_McData);
		Log.d(THIS_FILE, "checkPermission hasMcData = " + hasMcData  +" publishResult = "+ publishResult);
		if(!hasMcData)
		{
			return CauseCont.OperateErr_NoAuthorize_Data;
		}
		return CauseCont.OperateCheck_Suc;
	}
	public List<HistoryInfo> getHistoryInfoList()
	{
		return dbOperater.getHistoryInfoList();
	}
//	public HirstoryListLoader getHirstoryListLoader(Context ctx,HirstoryListLoaderCb callback)
//	{
//		return new HirstoryListLoader(ctx,callback);
//	}

	public SessionListLoader getSessionListLoader(Context ctx,AddrInfo fromInfo,SessionListLoaderCb callback)
	{
		dbOperater.resetSessionCount(fromInfo);
		return new SessionListLoader(ctx,fromInfo,dbOperater,callback);
	}
//	public SessionFuncListLoader getFuncSessionListLoader(Context ctx,SessionListLoaderCb callback)
//	{
//		return new SessionFuncListLoader(ctx,dbOperater,callback);
//	}
	public boolean hasNewMsg()
	{
		return dbOperater.hasNewMsg();
	}

	public boolean delHirstory(String receiverUId)
	{
		return dbOperater.delHirstory(receiverUId);
	}

	public boolean delAllSession(String peerAddrId)
	{
		return dbOperater.delAllSession(peerAddrId);
	}

	public boolean delSession(SessionInfo info)
	{
		return dbOperater.delSession(info);
	}

	public int sendMcDataMessage(SessionInfo info)
	{
		Log.d(THIS_FILE, "sendMcDataMessage info = " + info);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		boolean hasMcData = checkPublishResult(CallAttr.McType_McData);
		Log.d(THIS_FILE, "sendMcDataMessage hasMcData = " + hasMcData  +" publishResult = "+ publishResult);
		if(!hasMcData)
		{
			return CauseCont.OperateErr_NoAuthorize_Data;
		}
		AddrInfo addrInfo = info.getPeerAddrInfo();
		Log.d(THIS_FILE, "sendMcDataMessage addrInfo = " + addrInfo);
		String peerAddrId = addrInfo.getAddrId();
		int type = SessionInfo.Type_Normal;
		if(addrInfo.isFuncType())
		{
			type = SessionInfo.Type_Func;
		}
		MessageInfo msgInfo = info.toMessageInfo();
		try {
			if(info.isFdMsg())
			{
				result = service.sendFdMessage(type,msgInfo, peerAddrId);
			}
			else
			{
				result = service.sendSdsMessage(type,msgInfo, peerAddrId);
			}
			Log.d(THIS_FILE, "sendMcDataMessage result = " + result);
			if(result == CauseCont.OperateCheck_Suc)
			{
				dbOperater.insertNewSession(info,true);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.d(THIS_FILE, "sendMcDataMessage e = " + e);
		}
		return result;
	}

//	public int sendFdMessage(SessionInfo sessionInfo)
//	{
//		Log.d(THIS_FILE, "sendFdMessage sessionInfo = " + sessionInfo);
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//		boolean hasMcData = checkPublishResult(CallAttr.McType_McData);
//		Log.d(THIS_FILE, "sendFdMessage hasMcData = " + hasMcData  +" publishResult = "+ publishResult);
//		if(!hasMcData)
//		{
//			return CauseCont.OperateErr_NoAuthorize_Data;
//		}
////		if(!isAuthorize_Data)
////		{
////			return CauseCont.OperateErr_NoAuthorize_Data;
////		}
//		AddrInfo addrInfo = sessionInfo.getPeerAddrInfo();
//		Log.d(THIS_FILE, "sendFdMessage addrInfo = " + addrInfo);
//		String peerAddrId = addrInfo.getAddrId();
//		int type = SessionInfo.Type_Normal;
//		if(addrInfo.isFuncType())
//		{
//			type = SessionInfo.Type_Func;
//		}
//		MessageInfo msgInfo = sessionInfo.toMessageInfo();
//		try {
//			result = service.sendFdMessage(type,msgInfo, peerAddrId);
//			if(result == CauseCont.OperateCheck_Suc)
//			{
//				dbOperater.insertNewSession(sessionInfo,true);
//			}
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//			Log.d(THIS_FILE, "sendFdMessage e = " + e);
//		}
//		return result;
//	}

	public int sendNtfMessage(SessionInfo sessionInfo,byte status)
	{
		Log.d(THIS_FILE, "sendNtfMessage sessionInfo = " + sessionInfo + " status = " + status);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		boolean hasMcData = checkPublishResult(CallAttr.McType_McData);
		Log.d(THIS_FILE, "sendNtfMessage hasMcData = " + hasMcData  +" publishResult = "+ publishResult);
		if(!hasMcData)
		{
			return CauseCont.OperateErr_NoAuthorize_Data;
		}
//		if(!isAuthorize_Data)
//		{
//			return CauseCont.OperateErr_NoAuthorize_Data;
//		}
		AddrInfo peer = sessionInfo.getPeerAddrInfo();
		String peerMcdataId = peer.getAddrId();
		MessageInfo msgInfo = sessionInfo.toMessageInfo();
		msgInfo.setStatus(status);
		try {
			result = service.sendNtfMessage(msgInfo, peerMcdataId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.d(THIS_FILE, "sendNtfMessage e = " + e);
		}
		String conversationId = sessionInfo.getConversationId();
		String msgId = sessionInfo.getMsgId();

		dbOperater.updateSession(conversationId,msgId,status);

		return result;
	}

	public int updateSessionInfo(SessionInfo sessionInfo)
	{
		Log.d(THIS_FILE, "updateSessionInfo sessionInfo = " + sessionInfo);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		dbOperater.updateSession(sessionInfo);
		return result;
	}

	public int sendFdFindMessage()
	{
		Log.d(THIS_FILE, "sendFdFindMessage" );
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		try {
			result = service.sendFdFindMessage();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.d(THIS_FILE, "sendFdFindMessage e = " + e);
		}

		return result;
	}

	public void resetSessionCount(AddrInfo fromInfo)
	{
		dbOperater.resetSessionCount(fromInfo);
	}
	public void downloadFile(SessionInfo info,String url,String fileName,IHttpOpCb callback,OnDownloadListener listener)
	{
		DownloadFileLoader loader = new DownloadFileLoader(info,url,fileName,callback,listener);
		loader.downLoad();
	}
	public int uploadFile(final String url,File file,long len,final IHttpOpCb callback,OnUploadListener listener,String infoBody)
	{
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
		boolean hasMcData = checkPublishResult(CallAttr.McType_McData);
		Log.d(THIS_FILE, "sendFdMessage hasMcData = " + hasMcData  +" publishResult = "+ publishResult);
		if(!hasMcData)
		{
			return CauseCont.OperateErr_NoAuthorize_Data;
		}
		UploadFileLoader loader = new UploadFileLoader(url,file,len,callback,listener,infoBody);
		loader.upLoad();
		return CauseCont.OperateCheck_Suc;
	}
	public List<SessionReadStatus> getSessionReadStatusList(SessionInfo info)
	{
		return dbOperater.getSessionReadStatusList(info);
	}
	public void notification()
	{
		Log.d(THIS_FILE, "notification");
		if(service == null)
		{
			Log.e(THIS_FILE, "service == null ");
			return;
		}
		try
		{
			service.ring(2,true);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
